#include "userinterface.h"
#include "ui_userinterface.h"


UserInterface::UserInterface(QTcpSocket *client, const QString &user_name) :
    client(client), user_name(user_name),udp_room(NULL),
    ui(new Ui::UserInterface)
{
    ui->setupUi(this);
    ui->label_user_name->setText(user_name);
    selected_room_id.clear();
}


UserInterface::~UserInterface()
{
    if(udp_room != NULL){
        delete udp_room;
    }
    delete ui;
}

void UserInterface::setCreateRoomId(const QString &create_room_id)
{
    this->create_room_id = create_room_id;
}

void UserInterface::createRoom(QTcpSocket *client, const QString &user_name, const QString &create_room_id)
{
    // 显示创建的房间号
    ui->listWidget->addItem(create_room_id);

    // 隐藏当前窗口，显示直播间窗口
    // 创建房间，用户名和房主名是一样的
    udp_room = new UdpRoom(client, user_name, create_room_id, true, user_name);
    this->hide();
    udp_room->show();

}

void UserInterface::joinRoom(QTcpSocket *client, const QString &user_name, const QString &owner_name, const QString &join_room_id)
{
    udp_room = new UdpRoom(client, user_name, join_room_id, false, owner_name);
    this->hide();
    udp_room->show();
}

//创建房间
void UserInterface::on_pushButton_2_clicked()
{
    Pack pack;

    pack.setType(TYPE_CREATE_ROOM);
    client->write(pack.data(), pack.size());
}


// 加入房间
void UserInterface::on_pushButton_clicked()
{
    // 提示没有可供选择的房间
    if(selected_room_id.isEmpty() == true){
        QMessageBox::warning(this, "Join in room", "No room to selected...");
        return;
    }

    //将选择加入的房间号，发送给服务器。
    Pack pack;
    pack.setType(TYPE_JOIN_ROOM);
    pack.append(selected_room_id);
    client->write(pack.data(), pack.size());
}


// 点击房间号的时候触发
void UserInterface::on_listWidget_itemClicked(QListWidgetItem *item)
{
    selected_room_id = item->text();
}
